/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.limignmat.meteocal.rest;

import it.polimi.limignmat.meteocal.business.entity.User;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author luca
 */
@Stateless
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
public class UsersResource {
  @PersistenceContext(unitName = "MeteoCalPU",
                      type = PersistenceContextType.TRANSACTION)
  EntityManager entityManager;

  @GET
  @Path("/")
  public List<User> list() {
    return entityManager.createQuery("SELECT e FROM User e").getResultList();
  }

  @GET
  @Path("{id}")
  public User read(@PathParam("id") long id) {
    return entityManager.find(User.class, id);
  }

  @POST
  @Path("/register")
  public void register(String entity) {
    System.out.println("POST /register " + entity);
  }

  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  @Path("/create")
  public User create(User u) {
    entityManager.persist(u);
    return u;
  }
}
